package br.ufrj.ppgi.jemf_advanced_example.health_care_unit;

import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ScrollView;
import br.ufrj.ppgi.jemf.mobile.bean.HealthCareUnit;
import br.ufrj.ppgi.jemf.mobile.view.HealthCareUnitView;
import br.ufrj.ppgi.jemf_advanced_example.BuildConfig;

/**
 * A fragment representing a single HealthCareUnit detail screen.
 * This fragment is either contained in a {@link HealthCareUnitListActivity} in two-pane mode (on tablets) or a {@link HealthCareUnitDetailActivity} on handsets.
 */
public class HealthCareUnitDetailFragment extends Fragment {
    /**
     * The serialization (saved instance state) Bundle key representing the activated item position.
     * Only used on tablets.
     */
    private static final String STATE_HEATLH_CARE_UNIT = "activated_health_care_unit";
	/**
     * The fragment argument representing the item ID that this fragment represents.
     */
    public static final String ARG_ID = "HEALTHCAREUNIT";
    /**
     * The HealthCareUnit content this fragment is showing.
     */
    private static HealthCareUnit healthCareUnit;
    /**
     * The HealthCareUnit view holder.
     */
    private static HealthCareUnitView healthCareUnitView;



    /**
     * Mandatory empty constructor for the fragment manager to instantiate the fragment (e.g. upon screen orientation changes).
     */
    public HealthCareUnitDetailFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Load the HealthCareUnit content specified by the fragment arguments.
        if (getArguments().containsKey(HealthCareUnitDetailFragment.ARG_ID)) {
        	healthCareUnit = getArguments().getParcelable(HealthCareUnitDetailFragment.ARG_ID);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
	    // Show the HealthCareUnit content
	    if (healthCareUnit != null) {
	    	healthCareUnitView = new HealthCareUnitView(getActivity(), false);
	    	// Name
	    	healthCareUnitView.setHealthCareUnitNameText(healthCareUnit.getName());
	    	// Description
	    	healthCareUnitView.setHealthCareUnitDescriptionText(healthCareUnit.getDescription());
	    	// Capacity
	    	healthCareUnitView.setHealthCareUnitCapacityText(String.valueOf(healthCareUnit.getCapacity()));
	    	// Level
	    	healthCareUnitView.setHealthCareUnitLevelText(healthCareUnit.getLevelString());

	    	// Its necessary to add the HealthCareUnitView inside a ScrollView to show scrolls
	    	ScrollView scrollView = new ScrollView(getActivity());
	    	scrollView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
	    	// Add HealthCareUnit View as a child of the ScrollView.
	    	scrollView.addView(healthCareUnitView);
	    	// Add a Tag to the view location
	    	scrollView.setTag(ARG_ID);

			return scrollView;
    	} else {
        	return null;
    	}
    }

    @Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
        // Restore the previously serialized activated HealthCareUnit
        if (savedInstanceState != null && savedInstanceState.containsKey(STATE_HEATLH_CARE_UNIT)) {
        	healthCareUnit = savedInstanceState.getParcelable(STATE_HEATLH_CARE_UNIT);
        }
	}





	/**
     * Get updated HealthCareUnit
     * @return HealthCareUnit
     */
	public static HealthCareUnit getHealthCareUnit() {
		if (BuildConfig.DEBUG) Log.i(HealthCareUnitListActivity.TAG, "HealthCareUnitDetailFragment ::: getHealthCareUnit");
    	return healthCareUnit;
    }

	/**
	 * Update HealthCareUnit data from UI components
	 */
	public static void updateDataFromUI() {
    	if ((healthCareUnit != null) && (healthCareUnitView != null)) {
    		if (BuildConfig.DEBUG) Log.i(HealthCareUnitListActivity.TAG, "HealthCareUnitDetailFragment ::: updateDataFromUI");
    		// Update HealthCareUnit object with new data included by user through components
    		healthCareUnit.setName(healthCareUnitView.getHealthCareUnitNameText());
    		healthCareUnit.setDescription(healthCareUnitView.getHealthCareUnitDescriptionText());
    		healthCareUnit.setCapacity(Integer.valueOf(healthCareUnitView.getHealthCareUnitCapacityText()));
    		healthCareUnit.setLevelString(healthCareUnitView.getHealthCareUnitLevelText());
        }
    }



    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        // Force update data from UI
        updateDataFromUI();
        // Serialize and persist the activated HealthCareUnit.
        outState.putParcelable(STATE_HEATLH_CARE_UNIT, healthCareUnit);
    }
}
